package das_java_forum_ide;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTree;
import javax.swing.border.LineBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import java.awt.Color;
import java.io.File;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class NeuesVerzeichniss extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JTree tree;
	private DefaultTreeModel model;
	private JComboBox comboBox;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			NeuesVerzeichniss dialog = new NeuesVerzeichniss();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public NeuesVerzeichniss() {
		setTitle("Verzeichniss Erstellen");
		setBounds(100, 100, 511, 383);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.CENTER);
			panel.setLayout(null);
			
			JLabel lblNewLabel = new JLabel("Verzeichniss Name:");
			lblNewLabel.setBounds(10, 11, 110, 14);
			panel.add(lblNewLabel);
			
			textField = new JTextField();
			textField.setBounds(133, 8, 342, 20);
			panel.add(textField);
			textField.setColumns(10);
			
			JLabel lblNewLabel_1 = new JLabel("Projekt");
			lblNewLabel_1.setBounds(10, 42, 110, 14);
			panel.add(lblNewLabel_1);
			
			comboBox = new JComboBox();
			comboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					changeDir();
					
				}
			});
			comboBox.setBounds(133, 39, 342, 20);
			panel.add(comboBox);
			
			tree = new JTree();
			tree.setBorder(new LineBorder(new Color(0, 0, 0)));
			tree.setBounds(133, 70, 342, 203);
			panel.add(tree);
			
			model = (DefaultTreeModel) tree.getModel();
			
			JLabel lblbergeordnetesVerzeichniss = new JLabel("Verzeichniss:");
			lblbergeordnetesVerzeichniss.setBounds(10, 71, 110, 14);
			panel.add(lblbergeordnetesVerzeichniss);
			
			loadProjekts();
			
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						String path = comboBox.getSelectedItem().toString();
						TreePath np = tree.getSelectionPath();
						
						for(int i = 0 ; i < np.getPathCount() - 1 ; i++ ){
							path = path + "\\" + np.getPathComponent(i + 1);
						}
						
						System.out.println(path);
						
						File dir = new File(path);
						File f = new File(path + "\\" + textField.getText());
						if(dir.isDirectory()){
							f.mkdir();
						}
						
						Main.loadProjekts();
						
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	private void loadProjekts(){
		
		Projektverwaltung.Main pv = new Projektverwaltung.Main();
		ArrayList projects = pv.getProjekts(Main.WorkingDir);
		for(int i = 0 ; i < projects.size() ; i++ ){
			
			comboBox.addItem(projects.get(i));
			
		}
		
	}
	
	private void changeDir(){
		
		File f = new File(comboBox.getSelectedItem().toString());
		DefaultMutableTreeNode node = new DefaultMutableTreeNode(f.getPath());
		dirchanger.Main dc = new dirchanger.Main();
		dc.getdirs(node, f);
		model.setRoot(node);
		
		
		
	}
	
	
}
